package com.automobile.backstage.controller;

import com.automobile.backstage.domain.Announce;
import com.automobile.backstage.domain.Img;
import com.automobile.backstage.domain.Smart;
import com.automobile.backstage.factory.FileUploadFactory;
import com.automobile.backstage.service.ImgService;
import com.automobile.backstage.service.SmartService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-03-25 18:53
 */
@Controller
@RequestMapping("/smart")
public class SmartController {

    @Autowired
    private SmartService smartService;

    @Autowired
    private ImgService imgService;

    @RequestMapping("/findAll")
    public @ResponseBody PageInfo findAll(@RequestParam(value = "page",required = true,defaultValue = "1")Integer page,
                                          @RequestParam(value = "size",required = true,defaultValue = "10")Integer size) throws Exception {
        PageInfo pageInfo = null;
        List<Smart> smarts = smartService.findAll(page, size);
        pageInfo = new PageInfo(smarts);

        return pageInfo;
    }

    @RequestMapping("/del")
    public @ResponseBody int del(@RequestBody List<Integer> ids){
        try {
            smartService.delete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }


    @RequestMapping("/message")
    public @ResponseBody Smart message(@RequestBody Integer id){
        Smart smart = smartService.findById(id);
        return smart;
    }

    @RequestMapping("/lock")
    public @ResponseBody int updateStatus(@RequestBody String json){
        System.out.println(json);
        JsonParser jp = new JsonParser();
        JsonObject jo = jp.parse(json).getAsJsonObject();
        int id = Integer.parseInt(jo.get("id").toString());
        int status = jo.get("flag").toString().equals("true")?1:0;

        smartService.updateStatus(status,id);

        return 1;
    }

    /*
        跨服务器上传文件
     */
    @RequestMapping("/upload")
    public @ResponseBody int upload(@RequestParam("files") MultipartFile[] files,
                                    @RequestParam("jsonStr")String jsonStr) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Smart smart = mapper.readValue(jsonStr, Smart.class);
        System.out.println(smart);

        smartService.save(smart);

        if(files!=null&&files.length>0){

            //跨服务器上传
            FileUploadFactory.fileUpload(files,String.valueOf(smart.getId()),"2");

//            for(int i = 0;i<files.length;i++){
//                //文件名字
//                String fileName = files[i].getOriginalFilename();
//                fileName = fileName.substring(fileName.lastIndexOf("."));
//                //唯一值
//                String uuId = UUID.randomUUID().toString().replace("-", "");
//                //唯一值加上文件名字
//                fileName = uuId+"_"+fileName;
//                //保存图片
//                Img img = new Img();
//                img.setPath("files/"+fileName);
//                img.setSmartId(smart.getId());
//
//                imgService.save2(img);
//
//
//                // 创建客户端的对象
//                Client client = Client.create();
//
//                // 和图片服务器进行连接
//                WebResource webResource = client.resource(path + fileName);
//
//                // 上传文件
//                webResource.put(files[i].getBytes());
//            }

        }

        return 1;
    }


    /*
        跨服务器上传文件
     */
    @RequestMapping("/uploadUpdate")
    public @ResponseBody int uploadUpdate(@RequestParam("files") MultipartFile[] files,
                                    @RequestParam("jsonStr")String jsonStr) throws IOException {

        ObjectMapper mapper = new ObjectMapper();
        Smart smart = mapper.readValue(jsonStr, Smart.class);
        System.out.println(smart);
        smartService.update(smart);

        if(files!=null&&files.length>0){

            imgService.deleteBySmartId(smart.getId());

            //跨服务器上传
            FileUploadFactory.fileUpload(files,String.valueOf(smart.getId()),"2");

//            for(int i = 0;i<files.length;i++){
//
//                //文件名字
//                String fileName = files[i].getOriginalFilename();
//                fileName = fileName.substring(fileName.lastIndexOf("."));
//                //唯一值
//                String uuId = UUID.randomUUID().toString().replace("-", "");
//                //唯一值加上文件名字
//                fileName = uuId+"_"+fileName;
//                //保存图片
//                Img img = new Img();
//                img.setPath("files/"+fileName);
//                img.setSmartId(smart.getId());
//                imgService.save2(img);
//
//                // 创建客户端的对象
//                Client client = Client.create();
//                // 和图片服务器进行连接
//                WebResource webResource = client.resource(path + fileName);
//                // 上传文件
//                webResource.put(files[i].getBytes());
//            }
        }

        return 1;
    }

}
